﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace Webtextr
{
    public class StorageHelper
    {
        private const string FILE_NAME = "WebtextrSettings.xml";

        public StorageHelper()
        {
        }

        public UserSettings LoadSettings()
        {
            TextReader reader = null;
            UserSettings settings = null;
            try
            {
                IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream file = storage.OpenFile(FILE_NAME, FileMode.OpenOrCreate);
                reader = new StreamReader(file);

                XmlSerializer xs = new XmlSerializer(typeof(UserSettings));
                settings = (UserSettings)xs.Deserialize(reader);
                reader.Close();
            }
            catch
            {
            }
            finally
            {
                if (reader != null)
                    reader.Dispose();
            }
            return settings;
        }

        public void SaveSettings(UserSettings settings)
        {
            TextWriter writer = null;
            try
            {

                IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream file = storage.OpenFile(FILE_NAME, FileMode.Create);
                writer = new StreamWriter(file);
                XmlSerializer xs = new XmlSerializer(typeof(UserSettings));
                xs.Serialize(writer, settings);
                writer.Close();
            }
            catch
            {
            }
            finally
            {
                if (writer != null)
                    writer.Dispose();
            }
        }
    }
}
